package com.eshore.norm.pattern.singleton.demo2;

import java.io.PipedReader;

/**
 * 单例模式-懒汉式(静态内部类)
 *
 * @author: v-chenjl62
 * @date: 2024/1/30 15:47
 */
public class Singleton {
    //1.私有构造方法
    private Singleton() {
    }

    //2.创建本类对象
    // private static volatile Singleton singleton;
    private static class SingletonHolder {
        private static final Singleton SINGLETON = new Singleton();
    }

    //3.提供一个公共访问方法
    public static Singleton getInstance() {
        return SingletonHolder.SINGLETON;
    }

    //当进行反序列化时，会自动调用该方法，将该方法的返回值直接返回
    public Object readResolve(){
        return SingletonHolder.SINGLETON;
    }


}
